首页 新闻 搜索 专区 学院

java代码转化成c#代码 报400错误

0
悬赏园豆:50 [已关闭问题] 关闭于 2013-08-27 16:23

问题描述:java代码调用接口没问题,不管post的数据是正确的还是错误,都能有响应,但改为c#代码后报错:The remote server returned an error: (400) Bad Request.

代码大致如下:

java代码:

//hostURI: https://agcod-gamma-cn.amazon.com/CreateGiftCard

URL hostConnection = new URL(hostURI);

HttpURLConnection conn = (HttpURLConnection)

hostConnection.openConnection();

conn.setRequestMethod("POST");

//contentType = "application/x-www-form-urlencoded; charset=UTF-8"; conn.setRequestProperty("accept", contentType);

conn.setRequestProperty("content-type", contentType);

// host = "agcod-gamma-cn.amazon.com";

conn.setRequestProperty("host", host);

// 20130826T051152Z  根据当前时间生成的一个字符串

conn.setRequestProperty("x-amz-date" , dateTimeString);

//com.amazon.agcod.AGCODService

conn.setRequestProperty("x-amz-target", serviceTarget);

//一个加密的字符串,

conn.setRequestProperty("authentication", authorizationValue);

conn.setDoOutput(true);

OutputStream output = conn.getOutputStream();

output.write(payload.toString().getBytes("UTF-8"));

BufferedReader in = null;

String inputLine;

if (conn.getResponseCode() != 200)

{ in = new BufferedReader(new InputStreamReader(conn.getErrorStream())); } else { in = new BufferedReader(new InputStreamReader(conn.getInputStream())); }

System.out.println("\nRESPONSE:\n");

while ((inputLine = in.readLine()) != null)

{ System.out.println(inputLine );}

 转换的C#代码

  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(hostURI);
                request.Method = "POST";

           request.Accept = contentType;
            request.ContentType = contentType;
            request.Host = host;
            request.Headers.Add("x-amz-date", dateTimeString);
            request.Headers.Add("x-amz-target", serviceTarget);

            request.Headers.Add("authentication", authorizationValue);

         Stream output = request.GetRequestStream();
          byte[] contentBytes = Encoding.UTF8.GetBytes(payload.ToString());        
                output.Write(contentBytes, 0, contentBytes.Length);
                output.Close();
                string inputLine;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

当c#代码执行到最后一行的时候,报出400的错误:The remote server returned an error: (400) Bad Request. 

大家谁知道,这是什么问题呢,谢谢大家的帮助!

问题补充:

用fiddler工具测试:
post:https://agcod-gamma-cn.amazon.com/CreateGiftCard

Host: agcod-gamma-cn.amazon.com
Accept: application/x-www-form-urlencoded; charset=UTF-8
ContentType: application/x-www-form-urlencoded; charset=UTF-8
x-amz-date: 20130826T054311Z
x-amz-target: com.amazon.agcod.AGCODService
Content-Length: 0

响应的结果是:
HTTP/1.1 400 Bad Request
x-amzn-RequestId: bb5ad14f-0e15-11e3-9d3e-53596d1cb5a2
Content-Type: text/xml
Content-Length: 138
Date: Mon, 26 Aug 2013 06:07:05 GMT

<AGCODException>
  <Message>Invalid Date</Message>
  <errorType>GeneralError</errorType>
  <errorCode>F300</errorCode>
</AGCODException>

而c# HttpWebRequest调用直接抛出 400的异常。

王一一的主页 王一一 | 初学一级 | 园豆:133
提问于:2013-08-26 13:26
< >
分享
所有回答(3)
0

抓包看下,两段代码发出去的请求有什么差异。

Launcher | 园豆:45045 (高人七级) | 2013-08-26 13:36
0

//contentType = "application/x-www-form-urlencoded; charset=UTF-8"; conn.setRequestProperty("accept", contentType);

conn.setRequestProperty("content-type", contentType);

你这个唱的是哪出戏?注释?

content-type非常重要,看看你的远程服务器是接受application/x-www-form-urlencoded类型还是application/json类型?

Beyond-bit | 园豆:2824 (老鸟四级) | 2013-08-26 13:39

远程服务接收的是:application/x-www-form-urlencoded; charset=UTF-8

支持(0) 反对(0) 王一一 | 园豆:133 (初学一级) | 2013-08-26 13:46

远程服务接收的是:application/x-www-form-urlencoded; charset=UTF-8

java代码中是完整的参数  // 后面的值是相应参数的值。

支持(0) 反对(0) 王一一 | 园豆:133 (初学一级) | 2013-08-26 13:48
0

  HttpWebResponse response = request.GetResponse() as HttpWebResponse;

这行代码只要服务器返回任何不是200的。都认为是异常。异常代码中可加下面代码来捕获服务器返回的消息。

 WebException web = e as WebException;
                StreamReader intput = new StreamReader(web.Response.GetResponseStream());
                Console.WriteLine("\nRESPONSE:\n");
                string inputLine = string.Empty;
                while ((inputLine = intput.ReadLine()) != null)
                {
                    Console.WriteLine(inputLine);
                }

王一一 | 园豆:133 (初学一级) | 2013-08-27 16:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册