从android发送到asp.net中的ashx的http post请求。无法接收数据

发布于 2024-10-16 06:24:44 字数 3638 浏览 2 评论 0原文

我使用 http post 将数据从 android 发送到 asp.net 中的通用处理程序 .ashx 页面。但处理程序无法接收数据。这东西适用于 httpGet,但不适用于 httppost

android 代码

package com.postApp;
/*
 * HTTP POST and BasicNameValuePair
 * */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class postAct extends Activity {
    /** Called when the activity is first created. */

    class login{
        public
        String uname;
        public String pass;

    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


            login l=new login();


        HttpClient client1 = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://10.0.2.2:18089/POC_login/Handler.ashx");      


       l.uname="piyush";
       l.pass="gupta";
              List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3);
            postParameters.add(new BasicNameValuePair("uname", l.uname));
            postParameters.add(new BasicNameValuePair("pass", l.pass));
       try {
            request.setEntity(new UrlEncodedFormEntity(postParameters));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);

          HttpResponse response;
          response = client1.execute(request);

         BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         String line;
         String page="";
         line = in.readLine();
         while(line!=null)
         {
             page=page+line;
             line=in.readLine();
         }
         TextView tv = (TextView) findViewById(R.id.textview);
         tv.setText(page);
         in.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }                    
    }


}   

ashx 代码

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Linq;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) 
    {
        context.Request.ContentType = "text/plain";
       // context.Request.ContentType = "text/html";


        string username=context.Request.QueryString["uname"];
        string password = context.Request.QueryString["pass"];
        context.Response.Write("Hello Piyush");

        NorthwindDataContext db = new NorthwindDataContext();
        var found = (from p in db.Catergories
                    where p.cat_ID == 1
                    select p.cat_name).SingleOrDefault();

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

请帮助!

I am sending data from android to a Generic handler .ashx page in asp.net using http post. But the handler is not able to recieve data. The thing works with httpGet but not with httppost

The android code

package com.postApp;
/*
 * HTTP POST and BasicNameValuePair
 * */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class postAct extends Activity {
    /** Called when the activity is first created. */

    class login{
        public
        String uname;
        public String pass;

    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


            login l=new login();


        HttpClient client1 = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://10.0.2.2:18089/POC_login/Handler.ashx");      


       l.uname="piyush";
       l.pass="gupta";
              List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3);
            postParameters.add(new BasicNameValuePair("uname", l.uname));
            postParameters.add(new BasicNameValuePair("pass", l.pass));
       try {
            request.setEntity(new UrlEncodedFormEntity(postParameters));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
            request.setEntity(formEntity);

          HttpResponse response;
          response = client1.execute(request);

         BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
         String line;
         String page="";
         line = in.readLine();
         while(line!=null)
         {
             page=page+line;
             line=in.readLine();
         }
         TextView tv = (TextView) findViewById(R.id.textview);
         tv.setText(page);
         in.close();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }                    
    }


}   

The ashx code

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Linq;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) 
    {
        context.Request.ContentType = "text/plain";
       // context.Request.ContentType = "text/html";


        string username=context.Request.QueryString["uname"];
        string password = context.Request.QueryString["pass"];
        context.Response.Write("Hello Piyush");

        NorthwindDataContext db = new NorthwindDataContext();
        var found = (from p in db.Catergories
                    where p.cat_ID == 1
                    select p.cat_name).SingleOrDefault();

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

Please Help!

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

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

发布评论

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

评论(2

小苏打饼 2024-10-23 06:24:44

该问题可能是由于内容类型处理不正确造成的。

在Android端,您应该设置Content-type标头,例如:

request.addHeader("Content-type", "application/x-www-form-urlencoded");
HttpResponse response;
response = client1.execute(request);

然后您不应该覆盖服务器上的内容类型,即从ProcessRequest中删除以下内容:

context.Request.ContentType = "text/plain";
// context.Request.ContentType = "text/html";

如果仍然有问题,请务必检查并确保您的服务器配置为接收 POST。

The problem is likely due to incorrect handling of the content-type.

On the Android side, you should set the Content-type header, e.g.:

request.addHeader("Content-type", "application/x-www-form-urlencoded");
HttpResponse response;
response = client1.execute(request);

And then you should not override the content-type on your server, i.e. remove the following from ProcessRequest:

context.Request.ContentType = "text/plain";
// context.Request.ContentType = "text/html";

If you still have problems, be sure to check and make sure your server is configured to receive POSTs.

终弃我 2024-10-23 06:24:44

当你调用 ashx 时使用 GET

而不是 POST

when u call ashx use GET

not POST

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