如何向 Web 服务发送 HTTP POST?

发布于 2024-12-06 23:31:49 字数 500 浏览 2 评论 0原文

我需要对 Web 服务进行 HTTP 发布。

如果我将其放入像这样的 Web 浏览器中

http://server/ilwebservice.asmx/PlaceGPSCords?userid=99&longitude=-25.258&latitude=25.2548

,那么会将值存储到服务器上的数据库中。

在 Eclipse 中,使用 Android 的 Java 编程。URL 将如下所示 一样

http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+"

就像将 uidlng1lat1 分配为字符串

。我将如何运行它?

谢谢

I need to do a HTTP post to a web service..

If I place this into a web browser like this

http://server/ilwebservice.asmx/PlaceGPSCords?userid=99&longitude=-25.258&latitude=25.2548

then is stores the values to our DB on the server..

In Eclipse, using Java programming for android.. The url will look like

http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+"

with uid, lng1 and lat1 being assigned as strings..

How would I run this?

Thanks

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

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

发布评论

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

评论(4

不语却知心 2024-12-13 23:31:49
try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
                //do something with the response
                Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
            }
} catch (Exception e) {
e.printStackTrace();
}
try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1+";
    HttpGet get = new HttpGet(getURL);
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
                //do something with the response
                Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
            }
} catch (Exception e) {
e.printStackTrace();
}
染墨丶若流云 2024-12-13 23:31:49

对于 http post 使用名称值对。请参阅下面的代码 -

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("longitude", long1));
    nameValuePairs.add(new BasicNameValuePair("latitude", lat1));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    HttpResponse response = httpclient.execute(httppost);

For http post use name value pair. See below code -

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("longitude", long1));
    nameValuePairs.add(new BasicNameValuePair("latitude", lat1));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    HttpResponse response = httpclient.execute(httppost);
爱本泡沫多脆弱 2024-12-13 23:31:49

事实上我已经做了你要做的事。所以试试这个东西。您可以创建一个像我这样的方法,并通过纬度、经度传递您的 URL。这是返回给您的 HTTP 连接的响应。

public static int sendData(String url) throws IOException
    {
        try{
            urlobj = new URL(url);
            conn = urlobj.openConnection();
            httpconn= (HttpURLConnection)conn;
            httpconn.setConnectTimeout(5000);
            httpconn.setDoInput(true);
        }
        catch(Exception e){
            e.printStackTrace();}
        try{
            responseCode = httpconn.getResponseCode();}
        catch(Exception e){
            responseCode = -1;
            e.printStackTrace();
        }
        return responseCode;
    }

Infact I had done already what you are going to do. So try this stff. You can create a method like this what I have and pass your URL with lat, long. This is return you the response of the HTTP connection.

public static int sendData(String url) throws IOException
    {
        try{
            urlobj = new URL(url);
            conn = urlobj.openConnection();
            httpconn= (HttpURLConnection)conn;
            httpconn.setConnectTimeout(5000);
            httpconn.setDoInput(true);
        }
        catch(Exception e){
            e.printStackTrace();}
        try{
            responseCode = httpconn.getResponseCode();}
        catch(Exception e){
            responseCode = -1;
            e.printStackTrace();
        }
        return responseCode;
    }
很糊涂小朋友 2024-12-13 23:31:49

我不确定我是否理解你的问题,但如果我理解了,我认为你可以使用 java.net.UrlConnection :

URL url = new URL("http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1);
URLConnection conn = url.openConnection();
conn.connect();

I'm not sure I understood your question, but if I did I think you can use java.net.UrlConnection :

URL url = new URL("http://server/ilwebservice.asmx/PlaceGPSCords?userid="+uid+"&longitude="+lng1+"&latitude="+lat1);
URLConnection conn = url.openConnection();
conn.connect();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文