所以,我创建了一个HttpClient并使用发布数据HttpClient.PostAsync().
我设置了HttpContent使用
HttpContent content = new FormUrlEncodedContent(post_parameters); 其中post_parameters是键值对列表List<KeyValuePair<string, string>>
问题是,当HttpContent有一个很大的值(一个图像转换为base64要传输)我得到一个URL太长的错误.这是有道理的 - 因为网址不能超过32,000个字符.但是如何将数据添加到HttpContentif中呢?
请帮忙.
我刚开始在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)
所以,非常简单.如果有人有任何建议,请告诉我.谢谢!
这两个库之间的任何关系或区别.
我正在使用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)
有任何想法吗?
我必须使用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就完成了.
我需要从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) 我试图在使用C#和.NET 4.5的404错误的情况下确定response返回HttpClient的GetAsync方法.
目前我只能说出错误已经发生而不是错误的状态,如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)
我的问题是我无法处理异常并确定其状态以及出错的其他详细信息.
我如何正确处理异常并解释发生了什么错误?
我正在尝试使用urllib2 http客户端在python中创建下载进度条.我查看了API(以及谷歌),似乎urllib2不允许您注册进度挂钩.但是旧版已弃用的urllib确实具有此功能.
有谁知道如何使用urllib2创建进度条或报告钩子?或者是否有其他一些黑客可以获得类似的功能?
当我连接到我的网络服务以检索数据时,手机有时会断开连接,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邮件列表中此帖子的超时.
我将不得不在此时从计时器手动抛出超时异常.
我正在尝试做一个简单的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) httpclient ×10
java ×5
android ×4
c# ×3
https ×3
ssl ×3
.net ×1
.net-4.5 ×1
async-await ×1
bad-request ×1
http ×1
httpcontent ×1
progress-bar ×1
python ×1
urllib2 ×1