不允许 Android WCF 方法

发布于 2025-01-08 01:42:56 字数 817 浏览 0 评论 0原文

我有以下代码,应该通过 WCF 服务将两个字段插入到数据库中,但在 Eclipse 中测试时收到 405 Method Not allowed 错误。

WCF 可在 Fiddler 和我的测试 C# Web 客户端上运行,我仅通过手机从 Android 收到错误,这让我相信问题可能出在这段代码中!

HttpPost request = new HttpPost("http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle");

request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

谁能看到我错过了什么!

编辑

我正在 Fiddler 中发帖,如果我发帖:

http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle

一切正常。

干杯,

迈克。

I have the following code that should insert two fields into a database via a WCF Service but I get a 405 Method Not Allowed error when I test in Eclipse.

The WCF works from both Fiddler and my testing C# web client, I only get the error from Android via my phone, leading me to believe the problem may be in this code!

HttpPost request = new HttpPost("http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle");

request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);

Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

Can anyone see what I’m missing!

Edit

I'm doing a post in Fiddler and if I post:

http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle

It all works fine works.

Cheers,

Mike.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小矜持 2025-01-15 01:42:56

更新:按请求返回。下面的代码是我用来在 Android 上进行 JSON POST 的代码,它对我有用。

您正在 Android 代码中执行 HTTP POST。你也在fiddler中做POST还是在做GET?我问的原因是您没有随帖子发送正文,仅设置查询字符串。我已包含对 WCF 服务执行 JSON POST 操作的 Android 代码。

希望这有帮助...

如果您发布更多信息,我将很乐意提供帮助并更新答案。

    public JSONObject sendPOSTurl(String url, JSONObject data) throws JSONException 
    {  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpPost httppost = new HttpPost(url);
        HttpResponse response;  

        try 
        {  
            StringEntity se = new StringEntity(data.toString());

            httppost.setEntity(se);
            httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Content-type", "application/json");

            response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();  

            if (entity != null) 
            {  
                InputStream instream = entity.getContent();  
                String result = convertStreamToString(instream);  

                instream.close();  
                return new JSONObject(result);  
            }  
        } 
        catch(Exception e)
        {
            Log.e("REST", "Failed to POST data", e);
        }

        return null;  
    }

public static String convertStreamToString(InputStream is) 
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try 
    {
        while ((line = reader.readLine()) != null) 
        {
            sb.append(line + "\n");
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            is.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

Update: back by request. The code below is what I am using to make a JSON POST on Android and it is working for me.

You are doing an HTTP POST in your Android code. Are you doing a POST in fiddler too or are you doing a GET? The reason I ask is that you are not sending a body with your post, only setting the query string. I have included my Android code that does a JSON POST to a WCF service.

Hope this helps...

If you post more information I will be glad to help and update answer.

    public JSONObject sendPOSTurl(String url, JSONObject data) throws JSONException 
    {  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpPost httppost = new HttpPost(url);
        HttpResponse response;  

        try 
        {  
            StringEntity se = new StringEntity(data.toString());

            httppost.setEntity(se);
            httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Content-type", "application/json");

            response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();  

            if (entity != null) 
            {  
                InputStream instream = entity.getContent();  
                String result = convertStreamToString(instream);  

                instream.close();  
                return new JSONObject(result);  
            }  
        } 
        catch(Exception e)
        {
            Log.e("REST", "Failed to POST data", e);
        }

        return null;  
    }

public static String convertStreamToString(InputStream is) 
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try 
    {
        while ((line = reader.readLine()) != null) 
        {
            sb.append(line + "\n");
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            is.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    return sb.toString();
}
做个ˇ局外人 2025-01-15 01:42:56
http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle

url 看起来像 HttpGet

如果你想使用 HttpPost 你应该把你的参数放在 HttpEntity 中,并且你的参数必须是相应的内容类型,比如 json

http://www.mysite.com/myservice.svc/postname?fname=fred&lname=frognoggle

the url is looks like a HttpGet

if you want to use HttpPost you should put you param in HttpEntity and you param must be corresponding Content-type like json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文