标签: httpclient

使用HttpClient时如何在HttpContent中设置大字符串?

所以,我创建了一个HttpClient并使用发布数据HttpClient.PostAsync().

我设置了HttpContent使用

HttpContent content = new FormUrlEncodedContent(post_parameters); 其中post_parameters是键值对列表List<KeyValuePair<string, string>>

问题是,当HttpContent有一个很大的值(一个图像转换为base64要传输)我得到一个URL太长的错误.这是有道理的 - 因为网址不能超过32,000个字符.但是如何将数据添加到HttpContentif中呢?

请帮忙.

.net c# httpclient asp.net-web-api httpcontent

32
推荐指数
3
解决办法
5万
查看次数

Android HttpClient和HTTPS

我刚开始在Android中实现HTTPS连接.本质上,我正在尝试使用org.apache.http.client.HttpClient连接到服务器.我相信,在某些时候,我需要访问应用程序的密钥库,以便使用私钥授权我的客户端.但是,目前,我只是试图联系,看看会发生什么; 我一直收到HTTP/1.1 400 Bad Request错误.

尽管有许多例子(似乎没有一个对我有用),我似乎无法做出这方面的正面或反面.我的代码看起来像这样(BODY常量是XmlRPC):

 private void connect() throws IOException, URISyntaxException{

    HttpPost post     = new HttpPost(new URI(PROD_URL));
    HttpClient client = new DefaultHttpClient();

    post.setEntity(new StringEntity(BODY));
    HttpResponse result = client.execute(post);

    Log.d("MainActivity", result.getStatusLine().toString());

}
Run Code Online (Sandbox Code Playgroud)

所以,非常简单.如果有人有任何建议,请告诉我.谢谢!

ssl https android httpclient bad-request

31
推荐指数
1
解决办法
6万
查看次数

31
推荐指数
2
解决办法
2万
查看次数

Content-Length标头已存在

我正在使用Android中包含的Apache HttpClient(4.1)来执行HttpPut.我已经确认我只有1个内容长度标题.但是,每次发送请求时,都会收到有关已指定的Content-Length标头的协议异常.

HttpClient client = new DefaultHttpClient();
putMethod = new HttpPut(url + encodedFileName);
putMethod.addHeader(..)  //<-once for each header
putMethod.setEntity(new ByteArrayEntity(data));
client.execute(putMethod);  //throws Exception
Run Code Online (Sandbox Code Playgroud)

引起:org.apache.http.ProtocolException:org.apache.http.protocol.RequestContent.process(RequestContent.java:70)中已经出现的Content-Length头文件位于org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor的.java:290)

有任何想法吗?

java android httpclient

30
推荐指数
4
解决办法
3万
查看次数

如何使用HttpClient将带有JSON的DELETE发送到REST API

我必须使用HttpClient类向具有JSON内容的REST API服务发送删除命令,并且无法使其正常工作.

API调用:

DELETE /xxx/current
{
 "authentication_token": ""
}
Run Code Online (Sandbox Code Playgroud)

因为我无法在下面的语句中添加任何内容:

HttpResponseMessage response = client.DeleteAsync(requestUri).Result;
Run Code Online (Sandbox Code Playgroud)

我知道如何使用RestSharp来完成这项工作:

var request = new RestRequest {
    Resource = "/xxx/current",
    Method = Method.DELETE,
    RequestFormat = DataFormat.Json
};

var jsonPayload = JsonConvert.SerializeObject(cancelDto, Formatting.Indented);

request.Parameters.Clear();
request.AddHeader("Content-type", "application/json");
request.AddHeader ("Accept", "application/json");
request.AddParameter ("application/json", jsonPayload, ParameterType.RequestBody);

var response = await client.ExecuteTaskAsync (request);
Run Code Online (Sandbox Code Playgroud)

但我没有RestSharp就完成了.

c# httpclient portable-class-library

30
推荐指数
3
解决办法
3万
查看次数

如何在Java中以编程方式检查SSL证书到期日期

我需要从Java网站上的SSL证书中提取到期日期,应该支持可信和自签名证书,例如:1.trusted https://github.com 2.self-signed https://mms.nw .RU /

我已经将一些代码复制为:

import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SSLTest {

    public static void main(String [] args) throws Exception {
        // configure the SSLContext with a TrustManager
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
        SSLContext.setDefault(ctx);

        URL url = new URL("https://github.com");//https://mms.nw.ru
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean …
Run Code Online (Sandbox Code Playgroud)

java ssl https httpclient ssl-certificate

29
推荐指数
1
解决办法
3万
查看次数

如何在使用HttpClient.GetAsync()时确定404响应状态

我试图在使用C#和.NET 4.5的404错误的情况下确定response返回HttpClientGetAsync方法.

目前我只能说出错误已经发生而不是错误的状态,如404或超时.

目前我的代码我的代码如下所示:

    static void Main(string[] args)
    {
        dotest("http://error.123");
        Console.ReadLine();
    }

    static async void dotest(string url)
    {
        HttpClient client = new HttpClient();

        HttpResponseMessage response = new HttpResponseMessage();

        try
        {
            response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine(response.StatusCode.ToString());
            }
            else
            {
                // problems handling here
                string msg = response.IsSuccessStatusCode.ToString();

                throw new Exception(msg);
            }

        }
        catch (Exception e)
        {
            // .. and understanding the error here
            Console.WriteLine(  e.ToString()  );                
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法处理异常并确定其状态以及出错的其他详细信息.

我如何正确处理异常并解释发生了什么错误?

c# exception-handling httpclient async-await .net-4.5

29
推荐指数
1
解决办法
4万
查看次数

Python urllib2 Progress Hook

我正在尝试使用urllib2 http客户端在python中创建下载进度条.我查看了API(以及谷歌),似乎urllib2不允许您注册进度挂钩.但是旧版已弃用的urllib确实具有此功能.

有谁知道如何使用urllib2创建进度条或报告钩子?或者是否有其他一些黑客可以获得类似的功能?

python http urllib2 httpclient progress-bar

28
推荐指数
3
解决办法
2万
查看次数

Android UnknownHostException:有没有办法设置超时?

当我连接到我的网络服务以检索数据时,手机有时会断开连接,DNS混乱,等等.然后我得到一个UnknownHostException非常好的.

我想要做的是在这里寻找hostName时设置超时:

 response = httpclient.execute(httpget);
Run Code Online (Sandbox Code Playgroud)

我已经设定:

HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
Run Code Online (Sandbox Code Playgroud)

但他们似乎并不申请HostLookUp.有没有办法在主机查找时设置超时?

编辑
我刚刚发现用户无法修改nslookup hc-dev邮件列表中此帖子的超时.

我将不得不在此时从计时器手动抛出超时异常.

java android httpclient

28
推荐指数
1
解决办法
3390
查看次数

Android SSL HttpGet(无对等证书)错误或(对等关闭连接)错误

我正在尝试做一个简单的HttpGet来阅读网页.我有这个在iOS上工作,并通过http在Android上工作,但不是https.

url是一个内部网络IP和自定义端口,所以我可以使用路径来读取这样的http http://ipaddress:port/MyPage.html

HttpClient httpclient = new DefaultHttpClient(httpParameters);
                    HttpResponse response;
        String responseString = null;
        try {
            // Try connection
            HttpGet get = new HttpGet(params[0].path);
            get.addHeader("Authorization",
                    "Basic "
                            + Base64.encodeBytes(new String(params[0].username + ":" + params[0].password)
                                    .getBytes()));
        response = httpclient.execute(get);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            response.getEntity().writeTo(out);
            out.close();
            responseString = out.toString();
        } else {
            // Closes the connection.
            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }
    } catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException");
        this.e = e;
    } …
Run Code Online (Sandbox Code Playgroud)

java ssl https android httpclient

28
推荐指数
3
解决办法
4万
查看次数