我将Android Java App移植到C#,Windows商店应用程序。它必须向具有多部分内容,图像和一些键/值数据的服务器发送POST。 java代码:
HttpPost post = new HttpPost(SERVER);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart(filename, new ByteArrayBody(byteArray, "image/jpeg", filename + ".jpg"));
entity.addPart("usr", new StringBody(user));
entity.addPart("sid", new StringBody(session));
entity.addPart("action", new StringBody(method));
post.setEntity(entity);
和Windows应用商店代码:
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
ByteArrayContent byteContent = new ByteArrayContent(byteArray);
multipartContent.Add(byteContent, fileName);
multipartContent.Add(new StringContent(User), "usr");
multipartContent.Add(new StringContent(Session), "sid");
multipartContent.Add(new StringContent(method), "action");
但服务器响应不同的响应。它可能是什么?
答案 0 :(得分:0)
如果服务器通过user-agent确定输出,则尝试在两个Apps上设置相同的UserAgent。
http://developer.android.com/reference/android/net/http/AndroidHttpClient.html
// UserAgent taken from http://www.useragentstring.com/pages/Android%20Webkit%20Browser/
// Pick one you find suitable or use Firefox/Chrome UserAgents, whatever works for you
String userAgent = "Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
AndroidHttpClient httpClient = AndroidHttpClient.getInstance(userAgent);
httpClient.execute(post);
C#/。NET
// make sure to use same user agent in both apps
string userAgent = "Mozilla/5.0 (Linux; U; Android 2.3.4; fr-fr; HTC Desire Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("user-agent",userAgent);
如果服务器根据用户代理标头返回不同的结果,那么应该给出相同的结果。